package com.woniuxy.controller.goodAttribute;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.dto.GoodsPropDto;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.GoodsFiledException;
import com.woniuxy.exception.PawnUserException;
import com.woniuxy.param.SaveGoodsPropParam;
import com.woniuxy.param.UpdateGoodsPropParam;
import com.woniuxy.param.KeyAndIdParam;
import com.woniuxy.param.PageQueryParam;
import com.woniuxy.service.GoodsPropService;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 属性 前端控制器
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/goods-prop")
@Api(tags = "商品属性接口")
public class GoodsPropController {

    @Resource
    private GoodsPropService goodsPropService;

    /**
     * 更新属性
     *
     * @param updateGoodsPropParam
     * @return
     */
    @ApiOperation("更新属性")
    @PostMapping("/updateGoodsProp.do")
    public Result updateGoodsProp(@RequestBody @Validated UpdateGoodsPropParam updateGoodsPropParam, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        boolean b = goodsPropService.updateGoodsProp(updateGoodsPropParam);
        if (b)

            return Result.newInstance().setCode(200).setMsg("更新成功");
        else {
            throw new GoodsFiledException(CodeEnum.GOODS_PROP_UPDATE_FAIL.getMessage(), CodeEnum.GOODS_PROP_UPDATE_FAIL.getCode());

        }
    }

    /**
     * 添加新属性
     *
     * @param sgpp
     * @return
     */
    @ApiOperation("添加新属性")
    @PostMapping("/saveGoodsProp.do")
    public Result saveGoodsProp(@RequestBody @Validated SaveGoodsPropParam sgpp, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        if (goodsPropService.saveGoodsProp(sgpp))
            return Result.newInstance().setCode(200).setMsg("添加成功");
        else {
            throw new GoodsFiledException(CodeEnum.GOODS_PROP_SAVE_FAIL.getMessage(), CodeEnum.GOODS_PROP_SAVE_FAIL.getCode());

        }
    }


    /**
     * 根据三类id查询当下属性
     *
     * @param pqp
     * @return
     */
    @ApiOperation("根据三类id查询当下属性")
    @PostMapping("/listGoodsPropBySmallerId.do")
    public Result listGoodsPropBySmallerId(@RequestBody PageQueryParam<KeyAndIdParam> pqp) {

        BaseDto<GoodsPropDto> pad = goodsPropService.listGoodsPropBySmallerId(pqp);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(pad);
    }


    /**
     * 根据goods_prop_id查询具体属性属性
     *
     * @param goodsPropId
     * @return
     */
    @ApiOperation("根据goods_prop_id查询具体属性属性")
    @PostMapping("/getGoodsPropById.do/{goodsPropId}")
    public Result getGoodsPropById(@PathVariable Integer goodsPropId) {

        GoodsPropDto dto = goodsPropService.getGoodsPropById(goodsPropId);


        return Result.newInstance().setCode(200).setMsg("查询成功").setData(dto);
    }


    /**
     * 批量删除属性(逻辑删除)
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除属性(逻辑删除)")
    @PostMapping("/removeGoodsPropByIds.do")
    public Result removeGoodsPropByIds(@RequestBody List<Integer> ids) {
        if (goodsPropService.removeGoodsPropByIds(ids)) {
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }

        throw new PawnUserException(CodeEnum.ROLE_DELETE_FAIL.getMessage(), CodeEnum.ROLE_DELETE_FAIL.getCode());

    }
}

